﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using ProjTemp.Desktop.Common.ViewModel;
using System.Windows;
using System.ComponentModel.Composition;
using Mysoft.ServerMonitoring.OMKit.Infrastructure;
using ProjTemp.Desktop.Common.Events;
using ProjTemp.Common.Logging;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.ServiceLocation;
using ProjTemp.Desktop.Common;
using ProjTemp.Desktop.Common.MessageBox;
using ProjTemp.Desktop.Common.Utils;
using ProjTemp.Desktop.Common.Controls;
using ProjTemp.Desktop.Common.Container;

namespace Mysoft.ServerMonitoring.OMKit.TitleBar.ViewModels
{
    [Export(typeof(TitleBarViewModel))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    internal class TitleBarViewModel : VMBase<List<TitleBarItem>>
    {
        private TitleBarItem _selectedIcon;
        public TitleBarItem SelectedIcon
        {
            get { return this._selectedIcon; }
            set
            {
                this._selectedIcon = value;
                this.RaisePropertyChanged(() => this.SelectedIcon);
            }
        }

        [ImportingConstructor]
        public TitleBarViewModel(ILogger logger, IRegionManager regionManager, IEventAggregator eventAggregator, IServiceLocator serviceLocator)
            :base(logger, regionManager, eventAggregator, serviceLocator)
        {
            this._logger.LogType = typeof(TitleBarViewModel);

            var foundIcons = new List<TitleBarItem>();
            var dict = new ResourceDictionary { Source = new Uri(StyleConst.IconsUriAddress) };
            foundIcons.Add(new TitleBarItem(Guid.NewGuid().ToString(), 1, "主  页", "主  页", ViewTopics.HomePageTopic, (Visual)dict["appbar_home"]));
            foundIcons.Add(new TitleBarItem(Guid.NewGuid().ToString(), 2, "服务监控", "服务监控", ViewTopics.ServiceMonitoringTopic, (Visual)dict["appbar_monitor"]));
            foundIcons.Add(new TitleBarItem(Guid.NewGuid().ToString(), 3, "客户设置", "客户设置", ViewTopics.ClientSettingTopic, (Visual)dict["appbar_people_status"]));
            foundIcons.Add(new TitleBarItem(Guid.NewGuid().ToString(), 4, "预警处理", "预警处理", ViewTopics.AlertHandlingTopic, (Visual)dict["appbar_alert"]));
            foundIcons.Add(new TitleBarItem(Guid.NewGuid().ToString(), 5, "消息通知", "消息通知", ViewTopics.MessageNotifyTopic, (Visual)dict["appbar_email_hardedge"]));
            foundIcons.Add(new TitleBarItem(Guid.NewGuid().ToString(), 6, "文件上传管理", "文件上传管理", ViewTopics.UploadFileTopic, (Visual)dict["appbar_cloud_upload"]));
            foundIcons.Add(new TitleBarItem(Guid.NewGuid().ToString(), 7, "报告下载", "报告下载", ViewTopics.ReportDownloadTopic, (Visual)dict["appbar_graph_line_up"]));

            this._eventAggregator.GetEvent<TitleBarItemsChangedEvent>().Subscribe(TitleBarItemsChangedEventHandler, ThreadOption.UIThread);

            this.Model = foundIcons;
            //this.SelectedIcon = foundIcons.First();
            //this.RequestNavigation();
        }

        public void RequestNavigation()
        {
            if (_eventAggregator != null)
            {
                var payload = new NavigationRequest()
                {
                    NavigationType = NavigationType.NonModalWindow,
                    ContainerName = RegionNames.RightContentRegion,
                    Topic = SelectedIcon.Topic,
                    Callback = new Action<object>( (o) => {
                        var messageBoxService = this._serviceLocator.GetInstance<IMessageBox>();
                        var result = messageBoxService.Show("Hello, world!!", "Hello", ProjTemp.Desktop.Common.MessageBox.MessageBoxButton.OKCancel, ProjTemp.Desktop.Common.MessageBox.MessageBoxImage.None);
                    })
                };
                if (this.View != null)
                    payload.Owner = TreeHelper.FindAncestor(this.View, v => {return v is IContainer;});

                _eventAggregator.GetEvent<NavigationRequestEvent>().Publish(payload);
            }
        }

        private void TitleBarItemsChangedEventHandler(TitleBarItemsChanged e)
        {
            switch (e.Action)
            {
                case NotifyTitleBarItemsChangedAction.Added:
                    {
                        this.Model.AddRange(e.NewTitleBarItems);
                    }
                    break;
                case NotifyTitleBarItemsChangedAction.Removed:
                    break;
                case NotifyTitleBarItemsChangedAction.Replaced:
                    break;
                default:
                    break;
            }

            this.RaisePropertyChanged(() => this.Model);
        }
    }
}
